import {history, post, message, get} from '@/utils/utils'

const transfer = {
  namespaced: true,
  state: {
    total: 0,
    list: [],
    hasData: true,
    pageNum: 1,
    showPopup: false,
    targetAgentInfo: {},
    selectedMap: [],
    transferArr: [],

    transferInRecords: [],
    transferOutRecords: [],
    transferInRecordsPageNum: 1,
    transferOutRecordsPageNum: 1,
    hasTransferInRecords: true,
    hasTransferOutRecords: true
  },

  mutations: {
    save(state, { field, payload }) {
      if (Object.prototype.toString.call(payload) === '[object Object]') {
        state[field] = { ...state[field], ...payload }
      } else {
        state[field] = payload
      }
    }
  },

  actions: {
    getList({ commit, state }, {deviceType, pageNum = 1, search = ''}) {
      get({api: '/mat/matdeviceinfo/getDevicePage',
        data: {
          deviceType,
          deviceStatus: '01',
          current: pageNum,
          size: 10,
          search
        },
        success: ({data}) => {
          const {pages, records, total} = data
          const {list} = state
          let recordList = list

          if (pageNum > 1) {
            recordList = recordList.concat(records)
          } else {
            recordList = records || []
            commit('save', {field: 'hasData', payload: true})
          }

          if (pages === pageNum || pages === 0) commit('save', {field: 'hasData', payload: false})

          commit('save', {field: 'pageNum', payload: pageNum})
          commit('save', {field: 'total', payload: total})
          commit('save', {field: 'list', payload: recordList})
        }})
    },

    getRecords({ commit, state }, {deviceType, queryType, pageNum = 1, search}) {
      get({
        api: '/mat/mattransferinfo/getMatTransferInfoList',
        data: {
          deviceType,
          queryType,
          current: pageNum,
          size: 10,
          search
        },
        success: ({data}) => {
          const {pages, records} = data
          const {transferInRecords, transferOutRecords} = state
          let recordList = queryType === 'out' ? transferOutRecords : transferInRecords

          if (pageNum > 1) {
            recordList = recordList.concat(records)
          } else {
            recordList = records || []
            commit('save', {field: queryType === 'out' ? 'hasTransferOutRecords' : 'hasTransferInRecords', payload: true})
          }

          if (pages === pageNum || pages === 0) commit('save', {field: queryType === 'out' ? 'hasTransferOutRecords' : 'hasTransferInRecords', payload: false})

          commit('save', {field: queryType === 'out' ? 'transferOutRecordsPageNum' : 'transferInRecordsPageNum', payload: pageNum})
          commit('save', {field: queryType === 'out' ? 'transferOutRecords' : 'transferInRecords', payload: recordList})
        }
      })
    },

    getRecordDetail({ commit }, no) {
      return get({
        api: '',
        success: ({data}) => {
          commit('save', {field: 'detail', payload: data})
        }
      })
    },

    getAgentInfo({ commit }, agentMobile) {
      return post({ api: `/mat/matdeviceinfo/agentInfo?agentMobile=${agentMobile}`
        // success: ({data}) => {
        //   return data
        // }
      })
    },

    transferDevice({ commit }, payload) {
      post({
        api: '/mat/mattransferinfo/agentStartTkTransfer',
        data: payload,
        success: ({data}) => {
          commit('save', {field: 'selectedMap', payload: []})
          message.success({msg: '划拨成功', onClose: history.goBack})
        }
      })
    },

    transferDevice2({ commit }, payload) {
      post({
        api: '/mat/mattransferinfo/agentTkTransfer',
        data: payload,
        success: ({data}) => {
          commit('save', {field: 'selectedMap', payload: []})
          message.success({msg: '划拨成功', onClose: history.goBack})
        }
      })
    },

    cancelTransfer({ commit, state }, {uid, idx, currentTab}) {
      post({
        api: `/mat/mattransferinfo/revokeTransfer`,
        data: {uid},
        success: ({data}) => {
          message.success({msg: '操作成功', onClose: () => commit('save', {field: 'showPopup', payload: false})})
          const list = state[currentTab === 'import' ? 'transferInRecords' : 'transferOutRecords']

          list[idx].transferStatus = '02'
          commit('save', {field: currentTab === 'import' ? 'transferInRecords' : 'transferOutRecords', payload: list})
        }
      })
    },

    acceptTransfer({ commit, state }, {uid, idx, currentTab}) {
      post({
        api: '/mat/mattransferinfo/receiveTransfer',
        data: {uid},
        success: ({data}) => {
          message.success({msg: '操作成功', onClose: () => commit('save', {field: 'showPopup', payload: false})})
          const list = state[currentTab === 'import' ? 'transferInRecords' : 'transferOutRecords']

          list[idx].transferStatus = '01'
          commit('save', {field: currentTab === 'import' ? 'transferInRecords' : 'transferOutRecords', payload: list})
        }
      })
    }

  }
}

export default transfer
